package com.example.publishDemo.Tools;

import com.example.publishDemo.Model.Article;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Tool {
    public static List<Article> subList(List list, int start, int end) {
        if (list.size() > start) {
            if (list.size() > end) {
                return list.subList(start, end);
            }else {
                return list.subList(start, list.size());
            }
        }
        return null;
    }

    public static String getCurrentDateTime() {
        // 获取当前日期时间
        LocalDateTime currentDateTime = LocalDateTime.now();

        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化日期时间
        String formattedDateTime = currentDateTime.format(formatter);

        return formattedDateTime;
    }

    public static String getCurrentDate() {
        // 获取当前日期时间
        LocalDateTime currentDateTime = LocalDateTime.now();

        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 格式化日期时间
        String formattedDateTime = currentDateTime.format(formatter);

        return formattedDateTime;
    }

    public static List<String> getPastSevenDays() {
        List<String> pastSevenDays = new ArrayList<>();
        LocalDateTime currentDate = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        for (int i = 6; i >= 0; i--) {
            LocalDateTime pastDay = currentDate.minusDays(i); //minusDays 减去一天
            pastSevenDays.add(pastDay.format(formatter));
        }

        return pastSevenDays;
    }
    public static List<String> getPastFiveDays(){
        List<String> pastFiveDays = new ArrayList<>();
        LocalDate currentDay = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 0; i < 5; i++) {
            LocalDate pastDay = currentDay.minusDays(i);
            pastFiveDays.add(pastDay.format(formatter));
        }
        return pastFiveDays;
    }
}
